# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=just
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.40.0
pkgrel=1
pkgdesc="Just a command runner (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://just.systems'
msys2_repository_url='https://github.com/casey/just'
msys2_documentation_url='https://just.systems/man/en'
msys2_references=(
  'purl: pkg:cargo/just'
)
license=('spdx:CC0-1.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" 'git')
source=("git+${msys2_repository_url}.git#tag=${pkgver}")
sha256sums=('32942c1a0567dee17b7df341986bd593305520e8684025406151fba87fa73a98')

prepare() {
  cd "${_realname}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}"

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    cargo build --release --frozen
}

# run `just test` with pre-built binary. this will recompile everything with debug profile
check() {
  export PATH="${_realname}/target/release:${PATH}"

  "${_realname}/justfile" test
}

package() {
  cd "${_realname}"

  WINAPI_NO_BUNDLED_LIBRARIES=1 \
    cargo install \
      --offline \
      --no-track \
      --frozen \
      --path . \
      --root "${pkgdir}${MINGW_PREFIX}"

  target/release/just --man | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/man/man1/just.1"
  local _complete='target/release/just --completions'
  $_complete bash | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/just"
  $_complete zsh | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_just"
  $_complete fish | install -Dm644 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/just.fish"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
